package com.cheer.tool;


import cn.hutool.core.util.StrUtil;
import com.cheer.util.ToolUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 *  点赞数据导入工具
 *  读取视频点赞提供的基础数据
 *  生成SQL 收到导入到数据库
 */
public class VoteDateImport {

    // SQL开始执行ID
    int startId = 0;
    static String photoPath = "C:\\Users\\Administrator\\Desktop\\青年文明号";
    static String prefixPath = "vote1/";
    static String activityId = "'5'";
    static String categoryId = "'13'";

    /**
     * 执行查询
     * @param args
     */
    public static void main(String[] args) throws Exception {

        // 1. 将文件
        File file = new File(photoPath);
        List<List<String>> list = new ArrayList<>();
        getFiles(file, list);

        // 执行生成SQL命令
        for (int i = 22; i < list.size() + 22; i++) {

            List<String> strings = list.get(i - 22);
            String pictureName = strings.get(0);
            String wordName = strings.get(1);

            String personNumber = String.valueOf((i-21));
            personNumber = ToolUtils.getExVotePersonPersonNumber(personNumber);

            String insertExVotePerson = ToolUtils.getExVotePersonSql(i, activityId, categoryId, personNumber, wordName, pictureName , wordName);
            String insertExVotePersonExtra = ToolUtils.getExVotePersonExtraSql(i, wordName, wordName);

            System.out.println(insertExVotePerson + "\n");
            System.out.println(insertExVotePersonExtra + "\n");
        }

    }

    /**
     * 抽取图片 放置在统一文件夹下
     * @param file
     * @param list
     * @return
     * @throws Exception
     */
    public static List<List<String>> getFiles(File file, List<List<String>> list) throws Exception {

        if(file.isDirectory()){
            File[] files = file.listFiles();
            boolean isAllFile = Arrays.stream(files).allMatch(targetFile -> targetFile.isFile());
            if(isAllFile){
                // 过滤隐藏文件夹
                List<File> collect = Arrays.stream(files).filter(file2 -> file2.isHidden() == false).collect(Collectors.toList());
                File[] filesCopy = new File[collect.size()];
                collect.toArray(filesCopy);
                files = filesCopy;

                if(files.length == 2){

                    Optional<File> picture = Arrays.stream(files).filter(file1 -> {
                       if(cn.hutool.core.io.FileUtil.extName(file1) == null){
                           System.out.println(file1.getName());
                       }
                       return isPicture(cn.hutool.core.io.FileUtil.extName(file1));
                    }).findAny();
                    Optional<File> wordFile = Arrays.stream(files).filter(file1 -> isPicture(cn.hutool.core.io.FileUtil.extName(file1)) == false).findAny();
                    String pictureFileStr = "";
                    String wordFileStr = "";

                    if(wordFile.isPresent()){
                        wordFileStr = wordFile.get().getName();
                    }else{
                        throw new Exception("未找到word文件！" + file.getName());
                    }
                    if(picture.isPresent()){
                        // vote3/黑豹突击队.png
                        String pictureNewName = cn.hutool.core.io.FileUtil.cleanInvalid(cn.hutool.core.io.FileUtil.mainName(wordFileStr)) +"."+cn.hutool.core.io.FileUtil.extName(picture.get().getName());
                        pictureFileStr = prefixPath + pictureNewName;
                        String targetFile = "C:\\Users\\Administrator\\Desktop\\图片文件" + File.separator
                                + pictureNewName;
                        cn.hutool.core.io.FileUtil.copy(picture.get().getPath(), targetFile, true);
                    }else{
                        throw new Exception("未找到图片文件！" + file.getName());
                    }
                    List<String> fileList = new ArrayList<>();
                    fileList.add(pictureFileStr);
                    fileList.add(wordFileStr);
                    list.add(fileList);
                }else{
                    System.out.println( " files.length :" + files.length);
                    throw new Exception("当前指定的文件夹不对！" + file.getName());
                }

            }else{
                for (int i = 0; i < files.length; i++) {
                    getFiles(files[i], list);
                }
            }
        }
        return list;
    }


    /**
     * 判断文件是否为图片<br>
     * @param fileType 文件类型名称没有 ‘，’<br>
     * @return 检查后的结果<br>
     * @throws Exception
     */
    public static boolean isPicture(String  fileType) {

        fileType = fileType.toLowerCase();
        // 文件名称为空的场合
        if(StrUtil.isBlank(fileType)){
            // 返回不和合法
            return false;
        }
        // 声明图片后缀名数组
        String imgeArray [] = {"bmp", "gif", "jpeg", "jpg",  "png"};
        for (int i = 0; i <imgeArray.length; i++) {
            if(fileType.equals(imgeArray[i])){
                return true;
            }
        }
        return false;
    }



}
